<!DOCTYPE HTML>
<html>
<head>
    <title>罪名列表</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
    <table style="width:100%;">
        <tr>
            <td style="width:100%;">
                <a class="mini-button" onclick="onOk" iconCls="fa-save" plain="true">确定</a>
                <a class="mini-button" onclick="onCancel" iconCls="fa fa-close" plain="true">关闭</a>
                <span class="separator"></span>
            </td>
            <td style="white-space:nowrap;">
                <input id="key" class="mini-textbox" emptyText="请输入罪名" style="width:200px;"
                       onenter="onKeyEnter" />
                <a class="mini-button" iconCls="fa fa-search" plain="true" onclick="search">查询</a>
            </td>
        </tr>
    </table>
</div>
<div allowSortColumn="false" allowMoveColumn="false" allowResizeColumn="false" style="width:100%;height:440px;"
     id="datagrid1" class="mini-datagrid" idField="id" showPager="true"
     pageSize="10" ajaxType="get" url="/azbj/inmates/sentence/syscode/zuiming?codeType=2_12"
     showCheckBox="true" multiSelect="true">
    <div property="columns">
        <div type="checkcolumn">
        </div>
        <div field="codeId" type="indexcolumn" width="40">序号</div>
        <div field="codeName" width="340" headerAlign="center" allowSort="true">罪名名称</div>
    </div>
</div>
<script type="text/javascript">
    mini.parse();
    var grid = mini.get("datagrid1");
    grid.load();
    var codeIds = []
    var codeIdList = []
    var selected = []
    var selectedIds = {}
    var cids = codeIds;
    var flag = true;

    function initSelected(ids) {
        if (!ids) {
            return;
        }
        ids = ids.split(',');
        for (var i = 0; i < ids.length; i++) {
            selected.push(ids[i])
            selectedIds[ids[i]] = true
            if (flag) {
                codeIdList.push(ids[i]);
                if (i == ids.length - 1) {
                    flag = false;//确保在翻页的时候不会把从url传过来的codeIdye也加在数组里头
                }
            }
        }
    }

    grid.on('load', function (data) {  //点击进入罪名信息列表
        data = data.data
        cids = ${codeIds};
        if (cids != "") {
            initSelected(cids)
        }
        for (var i = 0; i < data.length; i++) {
            if (selectedIds[data[i].codeId]) {
                grid.select(data[i]);
            }
        }
    })

    grid.on('select', function (data) {
        codeIdList = removeRepeat(codeIdList);
        for (var i = 0; i < codeIdList.length; i++) {
            if (codeIdList[i].codeId == data.record.codeId) {
                return
            }
        }
        codeIdList.push(data.record.codeId);
        codeIdList = removeRepeat(codeIdList);
    })

    grid.on('deselect', function (data) {
        codeIdList = removeRepeat(codeIdList);
        for (var i = 0; i < codeIdList.length; i++) {
            if (codeIdList[i] == data.record.codeId) {
                codeIdList.splice(i, 1)
                delete codeIdList[data.record.codeId]
            }
        }
    })

    /*数组去重*/
    function removeRepeat(arr) {
        var newArr = [];
        for (var i = 0; i < arr.length; i++) {
            if (newArr.indexOf(arr[i]) == -1) {
                newArr.push(arr[i]);
            }
        }
        return newArr;
    }

    function search() {
        var key = mini.get("key").getValue();
        grid.load({queryKey: key});
    }

    function onKeyEnter(e) {
        search();
    }

    /*无需实现*/
    function SetData(data) {
        cids = data.codeId;
        mini.get("codeId").setValue(data.codeId);
    }

    // 获取项目的名称
    function GetData() {
        var ids = [];
        var names = [];
        // 去除选中的罪名
        var selected = grid.getSelecteds();
        if (selected.length > 0) {
            for (var i = 0, l = selected.length; i < l; i++) {
                if (selected[i].codeId > 0) {
                    names.push(selected[i].codeName);
                }
            }
        }
        var codeId = codeIds.join(',');
        var codeName = names.join(',');
        var data = {codeId: codeIdList, codeName: ''};
        return data;
    }

    function onOk() {
        CloseWindow("ok");
    }

    function onCancel() {
        CloseWindow("cancel");
    }

    function CloseWindow(action) {
        if (action == "close" && form.isChanged()) {
            if (confirm("数据被修改了，是否先保存？")) {
                return false;
            }
        }
        if (window.CloseOwnerWindow) {
            return window.CloseOwnerWindow(action);
        } else {
            window.close();
        }
    }
</script>
</body>
</html>